package org.ns.framework.datasource.mybatis;

import java.util.HashSet;
import java.util.Set;

import org.ns.framework.datasource.database.OperateResult;

public class CommitResult implements OperateResult {

	private Set<String> fails = new HashSet<>();
    private int reference;
    private Object result;
    public void addFailDataSource(String dataSourceName) {
        fails.add(dataSourceName);
    }

    public Set<String> getFailDataSources() {
        return fails;
    }

    public boolean commitOK() {
        return fails.size() == 0;
    }

    public CommitResult throwCommitException() {
        if (!commitOK()) {
            throw new CommitException(this);
        }
        return this;
    }
    
    protected void setReference(int reference) {
		this.reference = reference;
	}
    public int getReference() {
		return reference;
	}
    
    void setResult(Object result) {
		this.result = result;
	}
    public <R>R getResult() {
		return (R)result;
	}

    public class CommitException extends RuntimeException{
        CommitResult commitResult;

        public CommitException(CommitResult commitResult) {
            this.commitResult = commitResult;
        }

        public CommitResult getCommitResult() {
            return commitResult;
        }
    }
}
